﻿
var cal_Width = 280; //定义日历显示的宽度，至少140
var WebCalendar = new WebCalendar();
function WebCalendar() //初始化日历的设置
{
    this.regInfo = "WEB Calendar ver 3.0&#13;关闭的快捷键：[Esc]";
    this.dayShow = 35;                       //定义页面上要显示的天数,不能小于35,或大于39
    this.daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    this.day = new Array(this.dayShow);            //定义日历展示用的数组
    this.dayObj = new Array(this.dayShow);            //定义日期展示控件数组
    this.dateStyle = null;                     //保存格式化后日期数组
    this.objExport = null;                     //日历回传的显示控件
    this.eventSrc = null;                     //日历显示的触发控件
    this.inputDate = null;                     //转化外的输入的日期(d/m/yyyy)
    this.thisYear = new Date().getFullYear(); //定义年的变量的初始值
    this.thisMonth = new Date().getMonth(); //定义月的变量的初始值
    this.thisDay = new Date().getDate();     //定义日的变量的初始值
    this.today = this.thisDay + "/" + this.thisMonth + "/" + this.thisYear;   //今天(d/m/yyyy)
    //this.iframe = window.frames("meizzCalendarIframe"); //日历的 iframe 载体
    //    this.calendar = getObjectById("meizzCalendarLayer");  //日历的层
    this.dateReg = "";           //日历格式验证的正则式

    this.yearFall = 50;           //定义显示的年份下拉框的年差值，如果今年是2000年，这里设置为50，就显示1950－2050
    this.format = "yyyy-mm-dd"; //回传日期的格式
    this.timeShow = false;        //是否返回时间
    this.drag = true;         //是否允许拖动
    this.darkColor = "#95B7F3";    //控件的暗色
    this.lightColor = "#FFFFFF";    //控件的亮色
    this.btnBgColor = "#E6E6FA";    //控件的按钮背景色
    this.wordColor = "#000080";    //控件的文字颜色
    this.wordDark = "#DCDCDC";    //控件的暗文字颜色
    this.dayBgColor = "#F5F5FA";    //日期数字背景色
    this.todayColor = "#FF0000";    //今天在日历上的标示背景色
    this.DarkBorder = "#D4D0C8";    //日期显示的立体表达色

    this.yearOption = "";
    var yearNow = new Date().getFullYear();
    yearNow = (yearNow <= 1000) ? 1000 : ((yearNow >= 9999) ? 9999 : yearNow);
    var yearMin = (yearNow - this.yearFall >= 1000) ? yearNow - this.yearFall : 1000;
    var yearMax = (yearNow + this.yearFall <= 9999) ? yearNow + this.yearFall : 9999;
    yearMin = (yearMax == 9999) ? yearMax - this.yearFall * 2 : yearMin;
    yearMax = (yearMin == 1000) ? yearMin + this.yearFall * 2 : yearMax;
    for (var i = yearMin; i <= yearMax; i++) this.yearOption += "<option value='" + i + "'>" + i + "年</option>";
}

function calendar() //主调函数
{
    WebCalendar.thisYear = new Date().getFullYear();
    WebCalendar.thisMonth = new Date().getMonth();
    WebCalendar.thisDay = new Date().getDate();
    writeCalendar();
}

function writeCalendar(date) {
    var m = date.getMonth();
    var xx = new Date(date);
    var datetime1 = new Date(date);
    var datetime2 = new Date(date.setMonth(m + 1));
    var datetime3 = new Date(xx.setMonth(m + 2));

    var M1_Table = Fill_T(datetime1.getFullYear(), datetime1.getMonth());
    var M2_Table = Fill_T(datetime2.getFullYear(), datetime2.getMonth());
    var M3_Table = Fill_T(datetime3.getFullYear(), datetime3.getMonth());

    return M1_Table + M2_Table + M3_Table;
}



function SetDateStr_date(dateStr) {
    var date = dateStr.split("-");
    return new Date(parseInt(date[0]), parseInt(date[1]) - 1, parseInt(date[2]));
}

function ForMatDate(dateTime, format) {
    var o = {
        "M+": dateTime.getMonth() + 1, //month  
        "d+": dateTime.getDate(),      //day  
        "h+": dateTime.getHours(),         //hour 
        "m+": dateTime.getMinutes(),
        //minute        "s+" : this.getSeconds(),                 //second  
        "q+": Math.floor((dateTime.getMonth() + 3) / 3),         //quarter  
        "S": dateTime.getMilliseconds() //millisecond      
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (dateTime.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}


function Fill_T(y, m) {
    var M1 = new Array();
    WebCalendar.daysMonth[1] = (0 == y % 4 && (y % 100 != 0 || y % 400 == 0)) ? 29 : 28;
    var M1_count = WebCalendar.daysMonth[m];
    var M1_xq = new Date(y, m, 1).getDay();
    M1 = Fill_Array(M1_xq, M1_count);
    return Fill_Table(M1, y, m);
}

function Fill_Table(M_Array, y, m) {
    var strIframe = "";
    strIframe = "<table class=\"tableWeek\"  border=\"1\" width='" + cal_Width + "px' cellpadding=\"0\" cellspacing=\"0\">" +
                        "<tr align=\"center\"> " +
                            "<td rowspan=\"6\" style=\"width: 50px;\">" + y + "年" + (m + 1) + "月</td>" +
                            "<td style=\"width: 70px;\">选择周</td>" +
                            "<td>一</td>" +
                            "<td>二</td>" +
                            "<td>三</td>" +
                            "<td>四</td>" +
                            "<td>五</td>" +
                            "<td>六</td>" +
                            "<td height=\"20px\">日</td>" +
                            "</tr>";

    var row = M_Array.length / 7;  
    for (var r = 0; r < row; r++) {
        var random_i = Math.floor(Math.random() * 100000);
        strIframe += "<tr id=\"tr_" + random_i + "\"><td id=\"checkinput\"><input id=\"Checkbox\" type=\"checkbox\" name=\"Checkbox\" onclick=SetClClour('" + random_i + "'); /><label for=\"aa\">第" + eval(r + 1) + "周</label></td>";
        for (var c = 0; c < 7; c++) {

            if ((new Date(y, m, M_Array[r * 7 + c]).getTime() - SetDateStr_date(ForMatDate(new Date(), "yyyy-MM-dd"))) > 0) {

                strIframe += "<td class=outprev id='meizzDay" + M_Array[r * 7 + c] + "'>" + M_Array[r * 7 + c] + "</td>";

            } else if ((new Date(y, m, M_Array[r * 7 + c]).getTime() - SetDateStr_date(ForMatDate(new Date(), "yyyy-MM-dd"))) == 0) {

                strIframe += "<td class=outtoday id='meizzDay" + M_Array[r * 7 + c] + "'>" + M_Array[r * 7 + c] + "</td>";
            } else {

                strIframe += "<td class=outnext id='meizzDay" + M_Array[r * 7 + c] + "'>" + M_Array[r * 7 + c] + "</td>";
            }

            if (M_Array[r * 7 + c] == WebCalendar.daysMonth[m]) {
                m = m + 1;
            }
        }

        strIframe += "</tr>";
    }
    strIframe += "</table>";

    return strIframe;
}

function Fill_Array(w, daycount) {

    var xxtianshu = 0;
    var bctianshu = 0;
    var indexID = 0;
    var ay = new Array();

    if (w == 1) {//判断1号是否是星期一
        if ((daycount % 7) == 0) {//判断是否能被星期整除
            xxtianshu = daycount;
            bctianshu = 0;
            for (var i = 0; i < xxtianshu; i++) {
                ay[i] = i + 1;
            }
        }
        else {
            xxtianshu = daycount;
            bctianshu = 7 - (daycount % 7); //需要补充的天数
            for (var i = 0; i < xxtianshu; i++) {
                ay[i] = i + 1;
            }
            indexID = xxtianshu - 1;

            for (var i = 0; i < bctianshu; i++) {
                indexID = indexID + 1;
                ay[indexID] = i + 1;
            }
        }
    }
    else {
        var dygxingqiyi = 0; //距离下一个星期一还有几天
        if (w == 0) {
            dygxingqiyi = 1;
        }
        else {
            dygxingqiyi = 8 - w;
        }
        //var qishizhi = dygxingqiyi + 1; //起始值

        if (((daycount - dygxingqiyi) % 7) == 0) {
            xxtianshu = daycount - dygxingqiyi;
            bctianshu = 0;
            for (var i = 0; i < xxtianshu; i++) {
                ay[i] = i + 1;
            }
            indexID = xxtianshu - 1;

        }
        else {
            xxtianshu = daycount - dygxingqiyi;
            bctianshu = 7 - ((daycount - dygxingqiyi) % 7); //需要补充的天数
            for (var i = 0; i < xxtianshu; i++) {
                ay[i] = dygxingqiyi + i + 1;
            }
            indexID = xxtianshu - 1;

            for (var i = 0; i < bctianshu; i++) {
                indexID = indexID + 1;
                ay[indexID] = i + 1;
            }
        }
    }

    return ay;
}

function appendZero(n) { return (("00" + n).substr(("00" + n).length - 2)); } //日期自动补零程序

